package com.gexiaobao.pigeon.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.model.bean.ProvinceEntity
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityAddressBinding
import com.gexiaobao.pigeon.ui.adapter.AreaAddressAdapter
import com.gexiaobao.pigeon.ui.adapter.CityAddressAdapter
import com.gexiaobao.pigeon.ui.adapter.HotCityAdapter
import com.gexiaobao.pigeon.ui.adapter.ProvinceAdapter2
import com.gexiaobao.pigeon.viewmodel.SettingViewModel
import com.github.promeg.pinyinhelper.Pinyin
import com.github.promeg.tinypinyin.lexicons.android.cncity.CnCityDict
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.finish
import me.yokeyword.indexablerv.IndexableLayout

/**
 * @Author      : hxw
 * @Date        : 2024/5/22 13:15
 * @Describe    :
 */
class ActivityAddress : BaseActivity<SettingViewModel, ActivityAddressBinding>() {

    private lateinit var mAdapterPro: ProvinceAdapter2
    private lateinit var mCityAdapter: CityAddressAdapter
    private lateinit var mAreaAdapter: AreaAddressAdapter
    private val hotCityAdapter: HotCityAdapter by lazy { HotCityAdapter(arrayListOf()) }
    private var mProvinceData: ArrayList<ProvinceEntity> = arrayListOf()
    private var mCityData: ArrayList<ProvinceEntity> = arrayListOf()
    private var mAreaData: ArrayList<ProvinceEntity> = arrayListOf()

    private var isDistrict = false//是否是直辖市或特别行政区
    private var mProvinceId = 0
    private var mCityId = 0
    private var provinceName = ""
    private var cityName = ""
    private var areaName = ""

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "选择所在地区"

        mViewModel.getProvinceList()

        /** 省列表 */
        mDatabind.indexAble.setLayoutManager(LinearLayoutManager(this))
        // 多音字处理
        Pinyin.init(Pinyin.newConfig().with(CnCityDict.getInstance(this)))
        mAdapterPro = ProvinceAdapter2(this)
        mDatabind.indexAble.setAdapter(mAdapterPro)
        mDatabind.indexAble.setOverlayStyle_Center()
        //全字母排序。  排序规则设置为：每个字母都会进行比较排序；速度较慢
        mDatabind.indexAble.setCompareMode(IndexableLayout.MODE_FAST)

        /** 城市列表 */
        mDatabind.indexAbleCity.setLayoutManager(LinearLayoutManager(this))
        Pinyin.init(Pinyin.newConfig().with(CnCityDict.getInstance(this)))
        mCityAdapter = CityAddressAdapter(this)
        mDatabind.indexAbleCity.setAdapter(mCityAdapter)
        mDatabind.indexAbleCity.setOverlayStyle_Center()
        mDatabind.indexAbleCity.setCompareMode(IndexableLayout.MODE_FAST)

        /** 区列表 */
        mDatabind.indexAbleArea.setLayoutManager(LinearLayoutManager(this))
        Pinyin.init(Pinyin.newConfig().with(CnCityDict.getInstance(this)))
        mAreaAdapter = AreaAddressAdapter(this)
        mDatabind.indexAbleArea.setAdapter(mAreaAdapter)
        mDatabind.indexAbleArea.setOverlayStyle_Center()
        mDatabind.indexAbleArea.setCompareMode(IndexableLayout.MODE_FAST)

        /** 热门城市列表 */
        mDatabind.recyclerViewHotCity.layoutManager = GridLayoutManager(this, 4)
        mDatabind.recyclerViewHotCity.adapter = hotCityAdapter

        initAdapterClick()
    }

    private fun initAdapterClick() {
        mAdapterPro.setOnItemContentClickListener { _, _, _, entity ->
            mProvinceId = entity.id
            setIds(1, entity.name, entity.provinceId, entity.cityId)
            if (provinceName == "北京市" || provinceName == "上海市" || provinceName == "重庆市" || provinceName == "天津市" || provinceName == "香港特别行政区" || provinceName == "澳门特别行政区") {
                mViewModel.getAreaList(entity.provinceId, entity.cityId)
                isDistrict = true
            } else {
                isDistrict = false
                mViewModel.getCityList(entity.provinceId)
            }
        }

        mCityAdapter.setOnItemContentClickListener { _, _, _, entity ->
            mCityId = entity.id
            setIds(2, entity.name, entity.provinceId, entity.cityId)
        }

        mAreaAdapter.setOnItemContentClickListener { _, _, _, entity ->
            setIds(3, entity.name, entity.provinceId, entity.cityId)
            val intent = Intent()
            intent.putExtra("provinceId", mProvinceId)
            intent.putExtra("cityId", mCityId)
            intent.putExtra("areaId", entity.id)
            intent.putExtra("addressResult", "$provinceName$cityName$areaName")
            finish(intent)
        }

        hotCityAdapter.setOnItemClickListener { _, _, position ->
            isDistrict = true
            mProvinceId = hotCityAdapter.data[position].pId
            mCityId = hotCityAdapter.data[position].id
            provinceName = hotCityAdapter.data[position].pName
            cityName = hotCityAdapter.data[position].name
            setIds(4, hotCityAdapter.data[position].name, hotCityAdapter.data[position].provinceId, hotCityAdapter.data[position].cityId)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvProvince, mDatabind.tvCity, mDatabind.tvArea, mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> {
                    finish()
                }

                mDatabind.tvProvince -> {
                    setHideRecycler(1)
                }

                mDatabind.tvCity -> {
                    setHideRecycler(2)
                }

                mDatabind.tvArea -> {
                    setHideRecycler(3)
                }
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 获取省市区 */
        mViewModel.provinceAndMunicipalitiesResult.observe(this) { it ->
            dismissLoading()
            mProvinceData.clear()
            it.list.forEach {
                mProvinceData.add(ProvinceEntity(it.value, it.label))
            }
            mAdapterPro.setDatas(mProvinceData)
            setAdapterVisible(true, isCity = false, isArea = false)
        }

        /** 获取省份列表 */
        mViewModel.provinceListResult.observe(this) { it ->
            mProvinceData.clear()
            it.list.forEach {
                mProvinceData.add(ProvinceEntity(it.areaId, it.cityId, it.id, it.name, it.provinceId))
            }
            mAdapterPro.setDatas(mProvinceData)
            setAdapterVisible(true, isCity = false, isArea = false)

            hotCityAdapter.setList(it.hotCityList)
            hotCityAdapter.notifyDataSetChanged()
        }
        /** 获取城市列表 */
        mViewModel.cityListResult.observe(this) { it ->
            mCityData.clear()
            it.list.forEach {
                mCityData.add(ProvinceEntity(it.areaId, it.cityId, it.id, it.name, it.provinceId))
            }
            mCityAdapter.setDatas(mCityData)
            setAdapterVisible(false, isCity = true, isArea = false)
        }
        /** 获取地区列表 */
        mViewModel.areaListResult.observe(this) { it ->
            mAreaData.clear()
            it.list.forEach {
                mAreaData.add(ProvinceEntity(it.areaId, it.cityId, it.id, it.name, it.provinceId))
            }
            mAreaAdapter.setDatas(mAreaData)
            setAdapterVisible(false, isCity = false, isArea = true)
        }
    }

    private fun setHideRecycler(type: Int) {
        when (type) {
            1 -> {
                setAdapterVisible(true, isCity = false, isArea = false)
                mDatabind.tvProvince.setTextColor(ContextCompat.getColor(this, R.color.color_fe4542))
                mDatabind.tvCity.setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark))
                mDatabind.tvArea.setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark))
            }

            2 -> {
                setAdapterVisible(false, isCity = true, isArea = false)
                mDatabind.tvProvince.setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark))
                mDatabind.tvCity.setTextColor(ContextCompat.getColor(this, R.color.color_fe4542))
                mDatabind.tvArea.setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark))
            }

            3 -> {
                setAdapterVisible(false, isCity = false, isArea = true)
                mDatabind.tvProvince.setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark))
                mDatabind.tvCity.setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark))
                mDatabind.tvArea.setTextColor(ContextCompat.getColor(this, R.color.color_fe4542))
            }
        }
    }

    private fun setAdapterVisible(isProvince: Boolean, isCity: Boolean, isArea: Boolean) {
        mDatabind.indexAble.isVisible = isProvince
        mDatabind.indexAbleCity.isVisible = isCity
        mDatabind.indexAbleArea.isVisible = isArea
    }

    /** 设置省市区的id */
    private fun setIds(type: Int, name: String, pId: Int, cId: Int) {
        when (type) {
            1 -> {//省份
                provinceName = name
                cityName = ""
                areaName = ""
                mDatabind.tvProvince.text = provinceName
                setTextAndColor(mDatabind.tvCity)
                mDatabind.tvProvince.setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark))
                mDatabind.llAddress.isVisible = true
                mDatabind.tvProvince.isVisible = true
                mDatabind.tvArea.isVisible = false
            }

            2 -> {//城市
                cityName = name
                areaName = ""
                mDatabind.tvCity.text = cityName
                //获取-区
                mViewModel.getAreaList(pId, cId)
                setTextAndColor(mDatabind.tvArea)
                mDatabind.tvCity.setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark))
                mDatabind.llAddress.isVisible = true
                mDatabind.tvProvince.isVisible = true
                mDatabind.tvCity.isVisible = !isDistrict
            }

            3 -> {//地区
                areaName = name
                mDatabind.tvArea.isVisible = true
                mDatabind.tvArea.text = areaName
                mDatabind.tvArea.setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark))
                mDatabind.llAddress.isVisible = true
                mDatabind.tvProvince.isVisible = true
                mDatabind.tvCity.isVisible = !isDistrict
            }

            4 -> {//热门城市
                cityName = name
                provinceName = ""
                areaName = ""
                //获取-区
                mViewModel.getAreaList(pId, cId)
                mDatabind.tvProvince.isVisible = true
                mDatabind.tvProvince.text = provinceName
                mDatabind.tvProvince.setTextColor(ContextCompat.getColor(this, R.color.comm_text_gray_dark))
                mDatabind.tvCity.isVisible = false
                mDatabind.llAddress.isVisible = true
                setTextAndColor(mDatabind.tvArea)
            }
        }
    }

    private fun setTextAndColor(tv: AppCompatTextView) {
        tv.isVisible = true
        tv.text = "请选择"
        tv.setTextColor(ContextCompat.getColor(this, R.color.color_fe4542))
    }

}